import { createSlice } from '@reduxjs/toolkit'
import { mockQueryAllList } from '@/store/taskOa/actions/taskActions'
const taskSlice = createSlice({
	name: 'task',
	initialState: {
		taskList: null
	},
	reducers: {
		getAllTaskList: (state, action) => {
			state.taskList = action.payload
		},
		getCompleteTaskList: (state, action) => {
			state.taskList = state.taskList.filter((item) => item.state === 2)
		},
		getUnCompleteTaskList: (state, action) => {
			state.taskList = state.taskList.filter((item) => item.state === 1)
		},
		removeTask: (state, action) => {
			state.taskList = state.taskList.filter((item) => item.id !== action.payload)
		},
		updateTask: (state, action) => {
			let taskList = state.taskList
			state.taskList = taskList.map((item) => {
				if (item.id === action.payload) {
					item.state = 2
					item.complete = new Date().toLocaleString('zh-CN', { hour12: false })
				}
				return item
			})
		}
	}
})

//从taskSlice中导出action
export const { getAllTaskList, removeTask, updateTask, getCompleteTaskList, getUnCompleteTaskList } = taskSlice.actions
export const getAllTaskListAsync = () => {
	return async (dispatch) => {
		const res = await mockQueryAllList()
		dispatch(getAllTaskList(res.list))
	}
}

export default taskSlice.reducer
